// pages/group/info.js
var app = getApp();
Page({
  data:{
    num:1,
    id:0,
    groupInfo:[],
    groupMember:[]
  },
  onLoad:function(options){
    var that=this
    console.log(options)
    that.setData({
      id:options.gid
    });
   console.log(that.data.groupInfo)

  },
  onShow: function () {
    var id = this.id;
    var self = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Group/groupDetail',
      method: 'post',
      data: {
        uid: app.d.userId,
        gid: self.data.id,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success:function(res){
        console.log(res.data.leftTime)
        var timeStr = '';
        if(res.data.leftTime>0){
          var t = --res.data.leftTime
          var h =  Math.floor(t/60/60)
          var m = Math.floor((t-h*60*60)/60)
          var s = t%60;
          if(h<10) h = "0"+h;
          if(m<10) m = "0"+m;
          if(s<10) s = "0"+s;
          timeStr = h+':'+m+':'+s
          self.setTimeData(res.data.leftTime)
        }
        var groupMember=[];
        for (var i = res.groupNum - 1; i >= 0; i--) {
          if(res.groupMember[i]){
            groupMember[i]=res.groupMember[i]
          }else{
            groupMember[i]={}
          }
        }
        self.setData({
          groupInfo:res.data.data,
          groupMember:res.data.member,
          leftTime:timeStr
        })
        console.log(self.data.groupInfo)
      }
    })
  },
  setTimeData:function(time){
    var self = this;
    setInterval(function(){
        var t = --time;
        var h =  Math.floor(t/60/60);
        var m = Math.floor((t-h*60*60)/60);
        var s = t%60;
        if(h<10) h = "0"+h;
        if(m<10) m = "0"+m;
        if(s<10) s = "0"+s;
        var timeStr = h+':'+m+':'+s
      self.setData({
        leftTime:timeStr
      })
    }, 1000)
  },
  onShareAppMessage:function(options){
    console.log(options)
    var path = '/pages/group/detail?id='+this.goodsInfo.oid
    return {
        title: this.goodsInfo.name,
        path: path,
        success:function(res){
          console.log(path)
          console.log(res)
        }
      }
  },
  goToHome:function(){
    wx.redirectTo({
      url:'/pages/purchase/index'
    })
  },
  goToBuy:function(){
    app.goodsInfo = this.goodsInfo; 
    app.goodsInfo.id = this.goodsInfo.gid;
    app.num = this.data.num; 
    app.propValue = this.propValue; 
    app.goodsPrice = this.goodsInfo.gprice*this.data.num;
    app.buyType = 1;
    app.groupPid = this.goodsInfo.oid;
    if(this.goodsInfo.property){
      if(this.propValue &&(this.propValue.length==this.goodsInfo.property.length)){
        app.redirect('goods/payfor');
      }else{
        app.showToast(this,'请选择商品属性');
      }
    }else{
      app.redirect('goods/payfor');
    }
  },
  selectProp:function(e){
    var current = e.currentTarget.dataset;
    var pid = current.pid;
    var pname = current.pname;
    var name = current.name;
    var propValue = this.propValue ? this.propValue : [];
    propValue[pid] = {pname:pname,name:name};
    this.propValue = propValue;
    this.setData({
      propValue:propValue
    })
  },
  minus:function(){
    var num = this.data.num > 1 ? --this.data.num : 1
    this.setData({
      num : num
    })
  },
  plus:function(){
    var num = ++this.data.num
    this.setData({
      num : num
    })
  },
  showModal:function(e){
   var showModalStatus = e.currentTarget.dataset.statu== 'open' ? true : false;
    this.setData({
      showModalStatus:showModalStatus
    })
  },
  addShopCart: function(e){ //添加到购物车
    var that = this;
    var ptype = e.currentTarget.dataset.type;
    //当存在规格数据时
    if (this.data.HAVE) {
      this.setData({
        buff: ''
      })
      for (var i = 0; i < this.data.includeGroup[0].attrValueList.length; i++) {
        if (i != this.data.includeGroup[0].attrValueList.length - 1) {
          this.setData({
            buff: this.data.buff + this.data.includeGroup[0].attrValueList[i].id + ','
          })
        } else {
          console.log(this.data.includeGroup[0].attrValueList[i].id)
          this.setData({
            buff: this.data.buff + this.data.includeGroup[0].attrValueList[i].id
          })
        }
      }
      var value = [];
      for (var i = 0; i < this.data.attrValueList.length; i++) {
        if (!this.data.attrValueList[i].selectedValue) {
          break;
        }
        value.push(this.data.attrValueList[i].selectedValue);
      }
      if (i < this.data.attrValueList.length) {
        wx.showToast({
          title: '请选择完整！',
          icon: 'loading',
          duration: 1000
        })
      } else {
        that.setData({
          tids: []
        })
        for (var i = 0; i < that.data.includeGroup[0].attrValueList.length; i++) {
          console.log("www" + i)
          if (that.data.includeGroup[0].attrValueList[i].tid) {
            that.setData({
              tids: that.data.tids.concat(that.data.includeGroup[0].attrValueList[i].tid)
            })
            console.log("qqq" + that.data.tids)
          }
        }
        wx.request({
          url: app.d.ceshiUrl + '/Api/Shopping/addGroup',
          method: 'post',
          data: {
            uid: app.d.userId,
            pid: that.data.groupInfo.pid,
            num: that.data.num,
            buff: that.data.buff,
            tid: that.data.tids,
            liji: 1,
            type: (ptype == 'buynow' ? 'buy' : '')
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          success: function(res) {
            var data = res.data;
            if (data.status == 1) {
              var DATAs = that.data.buff + ',' + data.cart_id
              if (ptype == 'buynow') {
                wx.navigateTo({
                  url: '/pages/order/pay?DATAs=' + DATAs+'&group_flag=2&group_id='+that.data.id
                });
                return;
              }
            } else {
              wx.showToast({
                title: data.err,
                duration: 2000
              });
            }
          },
          fail: function() {
            wx.showToast({
              title: '网络不给力！',
              duration: 2000
            });
          }
        });
      }
    } else {
      //当不存在规格数据时
      wx.request({
        url: app.d.ceshiUrl + '/Api/Shopping/addGroup',
        method: 'post',
        data: {
          uid: app.d.userId,
          pid: that.data.groupInfo.pid,
          num: that.data.num,
          liji: 1,
          type: (ptype == 'buynow' ? 'buy' : '')
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
            var data = res.data;
          if (data.status == 1) {
            if (ptype == 'buynow') {
              wx.redirectTo({
                url: '/pages/order/pay?cartId=' + res.data.cart_id+'&group_flag=2&group_id='+that.data.id
              });
              return;
            }
          } else {
            wx.showToast({
              title: data.err,
              duration: 2000
            });
          }
        },
        fail: function() {
          wx.showToast({
            title: '网络不给力！',
            duration: 2000
          });
        }
      });
    }
  },
})